// -----------------------------------------------------------------------------
// This source file is part of Matrix Platform
// 	(Universal .NET Software Development Platform)
// For the latest info, see http://www.matrixplatform.com
// 
// Copyright (c) 2009-2010, Ingenious Ltd
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Matrix.Common.Extended
{
    /// <summary>
    /// Class helps with operations on system performance counters.
    /// </summary>
    public class PerformanceCounterHelper
    {
        static bool _countersAllowed = true;
        
        /// <summary>
        /// Some environments should not use those.
        /// </summary>
        public static bool CountersAllowed
        {
            get { return PerformanceCounterHelper._countersAllowed; }
            set { PerformanceCounterHelper._countersAllowed = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public PerformanceCounterHelper()
        {
            //CounterCreationData xcounter = new CounterCreationData();
            //xcounter.CounterName = "Xtreme counter";
            //xcounter.CounterType = PerformanceCounterType.NumberOfItems32;
            //counters.Add(xcounter);

            //string categoryName = Application.ProductName + ".TestCounters1";
            //if (PerformanceCounterCategory.Exists(categoryName) == false)
            //{
            //    CounterCreationDataCollection counters = new CounterCreationDataCollection();

            //    PerformanceCounterCategory category = PerformanceCounterCategory.Create(categoryName, 
            //        "Category for the counters of " + Application.ProductName, PerformanceCounterCategoryType.SingleInstance,
            //        counters);

            //    CounterCreationData xcounter = new CounterCreationData();
            //    xcounter.CounterName = "Xtreme counter";
            //    xcounter.CounterType = PerformanceCounterType.NumberOfItems32;

            //    counters.Add(xcounter);

            //    //category.
            //}


            //PerformanceCounter _TotalOperations = new PerformanceCounter();
            //_TotalOperations.
            //_TotalOperations.CategoryName = "MyCategory";
            //_TotalOperations.CounterName = "Xtreme counter";
            //_TotalOperations.MachineName = ".";
            //_TotalOperations.ReadOnly = false;
        }

        public static PerformanceCounter CreateCounter(string categoryName, string name, string description, PerformanceCounterType type)
        {
            if (_countersAllowed == false)
            {
                return null;
            }

            //string categoryName = Application.ProductName + "." + name;
            if (PerformanceCounterCategory.Exists(categoryName) == false)
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection();

                CounterCreationData counterData = new CounterCreationData();
                counterData.CounterName = name;
                counterData.CounterHelp = description;
                counterData.CounterType = type;

                counters.Add(counterData);

                PerformanceCounterCategory category = PerformanceCounterCategory.Create(categoryName, "Category for the counters of " + 
                                                                                                      categoryName, PerformanceCounterCategoryType.SingleInstance, counters);
            }


            PerformanceCounter counter = new PerformanceCounter(categoryName, name, string.Empty, false);
            return counter;
        }
    }
}
